package gmail_test;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class LoginPage extends Page{

	public UserData admin = new UserData("gfyedfyyz@gmail.com","123gfyedfyyz");
	
	public LoginPage(WebDriver driver) 
	{
		super(driver);
	}

	@FindBy(id = "Email")
	public WebElement email;

	@FindBy(id = "Passwd")
	public WebElement passwd;

	@FindBy(id = "signIn")
	public WebElement buttonSignIn;
		
	//@FindBy(xpath="id('account-chooser-link')")
	public WebElement chooseAccount;
		
	@FindBy(xpath="id('account-chooser-add-account')")
	public WebElement addNewAccount;
	
	@Override
	public void Open() {
		driver.get(ConfigProperties.getProperty("login.url"));
		
		
		try
		{
		  chooseAccount = driver.findElement(By.xpath("id('account-chooser-link')"));
		  if(chooseAccount.isDisplayed())
				ReLogin();
		}
		catch(NoSuchElementException e)
		{}
		
		
	}

	public void ReLogin()
	{
			chooseAccount.click();
			PageFactory.initElements(driver, addNewAccount);
			addNewAccount.click();
	}
	
	public HomePage loginAs(UserData admin)
	{
		type(email, admin.user);
		type(passwd, admin.password);
		buttonSignIn.click();

		return PageFactory.initElements(driver, HomePage.class);
	}
}
